/*CopyRight 2015 By YXKJ Co., Ltd. All Rights Reserved.*/
package com.yx.system.util;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.wb.swt.ResourceManager;

import com.yx.ehis.system.Activator;

/**
 * 系统工具
 * 
 * @author zgq
 */
public class SystemTools {
	/**
	 * 根据视图编号显示视图
	 * 
	 * @param viewId
	 *            视图编号
	 * @return 显示后的视图对象
	 */
	public static IViewPart showView(String viewId) {
		IViewPart iViewPart = null;
		IWorkbenchWindow window = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow();
		try {
			iViewPart = window.getActivePage().showView(viewId);
		} catch (PartInitException e) {
			e.printStackTrace();
			MessageDialog.openError(SystemTools.getCurrentShell(), "错误提示：",
					"该视图不存在！");
		}
		return iViewPart;
	}

	/**
	 * 根据对话框的有效名称显示对话框
	 * 
	 * @param dialogQualifiedName
	 *            对话框的有效名称
	 * @return 显示后的对话框对象
	 */
	public static Dialog showDialog(String dialogQualifiedName) {
		Dialog dialog = null;
		try {
			Class<?> dialogClass = Class.forName(dialogQualifiedName);
			dialog = (Dialog) dialogClass.newInstance();
			dialogClass.getMethod("open").invoke(dialog);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		}
		return dialog;
	}

	/**
	 * 获取当前Shell
	 * 
	 * @return 当前Shell
	 */
	public static Shell getCurrentShell() {
		return Display.getCurrent().getActiveShell();
	}

	/**
	 * 根据路径获取图片对象
	 * 
	 * @param path
	 *            路径
	 * @return 图片对象
	 */
	public static Image getImage(String path) {
		return ResourceManager.getImage(Activator.getImageDescriptor(path));
	}
}
